const path = require("path");
const webpack = require('webpack');
const CopyWebpackPlugin = require("copy-webpack-plugin");
const { defines, info } = require('../base/defines');
const HtmlWebpackPlugin = require("html-webpack-plugin");

const cesiumSource = 'node_modules/cesium/Source';
const cesiumWorkers = "../Build/Cesium/Workers";
const cesiumBaseUrl = "cesiumStatic";

module.exports = {
    entry: {
        app: './src/index.ts'
    },
    output: {
        path: path.resolve("./dist/"),
        filename: "earthsdk2-cesium.js",
        library: "earthsdk2czm",
        libraryTarget: "umd",
    },
    amd: {
        toUrlUndefined: true
    },
    resolve: {
        extensions: ['.ts', '.js', '.json'],
        fallback: { path: require.resolve("path-browserify") },
        alias: {
            '@': path.resolve('./src/'),
            cesium: path.resolve('node_modules/cesium/Source/Cesium'),
            cesiumWidgets: path.resolve('node_modules/cesium/Source/Widgets'),
        },
    },
    module: {
        rules: [{
            test: /.ts$/,
            loader: "ts-loader",
            exclude: /node_modules/,
        }, {
            test: /\.css$/,
            use: ["style-loader", "css-loader"],
        },
        {
            test: /\.(png|gif|jpg|jpeg|svg|xml|json)$/,
            type: "asset/inline",
        }],
    },

    plugins: [
        new webpack.BannerPlugin(`${info.name}(${info.version}-${info.commitId.slice(0, 8)}-${info.date}) 版权所有@${info.owner}`),
        new webpack.DefinePlugin(defines),
        new HtmlWebpackPlugin({
            template: './index.html'
        }),
        new CopyWebpackPlugin({
            patterns: [
                { from: path.join(cesiumSource, cesiumWorkers), to: `${cesiumBaseUrl}/Workers`, },
                { from: path.join(cesiumSource, "ThirdParty"), to: `${cesiumBaseUrl}/ThirdParty`, },
                { from: path.join(cesiumSource, "Assets"), to: `${cesiumBaseUrl}/Assets`, },
                { from: path.join(cesiumSource, "Widgets"), to: `${cesiumBaseUrl}/Widgets`, },
            ],
        }),
        new webpack.DefinePlugin({
            'CESIUM_BASE_URL': JSON.stringify(cesiumBaseUrl)
        })
    ],
};


